﻿using System.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Home_School
{
    public partial class frmstudentEditer : Form
    {
        public frmstudentEditer()
        {
            InitializeComponent();
        }
        public SqlConnection con;
        public SqlCommand cmd;
        void cn()
        {
            con = new SqlConnection("server=.; uid=sa; pwd=123; database=student_info_db;");
            con.Open();
        }
        void loadsex()
        {
            cn();
            cmd = new SqlCommand("select * from tblsex", con);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();
            this.cboSex.DisplayMember = "sex";
            this.cboSex.ValueMember = "s_id";
            this.cboSex.DataSource = dt;

            con.Close();
        }
        void loadProvince()
        {
            cn();
            cmd = new SqlCommand("select * from tblProvince", con);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();
            this.cboProvince.DisplayMember = "province";
            this.cboProvince.ValueMember = "pro_id";
            this.cboProvince.DataSource = dt;

            con.Close();
        }
        void loadstatus()
        {
            cn();
            cmd = new SqlCommand("select * from tblStatus", con);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();
            this.cboStatus.DisplayMember = "status";
            this.cboStatus.ValueMember = "st_id";
            this.cboStatus.DataSource = dt;

            con.Close();
        }
        void loadsub()
        {
            cn();
            cmd = new SqlCommand("select * from tblsubject", con);
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dr.Close();
            this.cboSub_name.DisplayMember = "sub_name";
            this.cboSub_name.ValueMember = "sub_id";
            this.cboSub_name.DataSource = dt;
            con.Close();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            cn();
            if (txtCode.Text == "")
                MessageBox.Show("You look like not enter value on Text box Studen Code");
               
            else if (txtKh_name.Text == "")
                MessageBox.Show("You look like not enter value on Text box Studen Name in khmer");
            else 
         
            cmd = new SqlCommand("insert into tblstudent values (@sub_id, @s_id, @st_id, @pro_id, @stu_code, @stu_name_kh, @stu_name_eng, @dob, @adr, @phone, @email, @other)",con);

            cmd.Parameters.Add("@sub_id", SqlDbType.Int).Value = txtsub_id.Text;
            cmd.Parameters.Add("@s_id", SqlDbType.Int).Value = txts_id.Text;
            cmd.Parameters.Add("@st_id", SqlDbType.Int).Value = txtst_id.Text;
            cmd.Parameters.Add("@pro_id", SqlDbType.Int).Value = txtpro_id.Text;
            cmd.Parameters.Add("@stu_code", SqlDbType.NVarChar).Value = txtCode.Text;
            cmd.Parameters.Add("@stu_name_kh", SqlDbType.NVarChar).Value = txtKh_name.Text;
            cmd.Parameters.Add("@stu_name_eng", SqlDbType.NVarChar).Value = txtname_eng.Text;
            cmd.Parameters.Add("@dob", SqlDbType.NVarChar ).Value = mtxtdob.Text;
            cmd.Parameters.Add("@adr", SqlDbType.NVarChar).Value = txtAddress.Text;
            cmd.Parameters.Add("@phone", SqlDbType.NVarChar).Value = txtPhone.Text;
            cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = txtEmail.Text;
            cmd.Parameters.Add("@other", SqlDbType.NVarChar).Value = txtother.Text;
            cmd.ExecuteNonQuery();
            con.Close();
            this.Close();
            
            }
        private void frmstudentEditer_Load(object sender, EventArgs e)
        {
           //this.WindowState = FormWindowState.Maximized;
            this.loadsex();
            this.loadsub();
            this.loadProvince();
            this.loadstatus();

            if (txts_id.Text == "")
            {
                btnSave.Text = "រក្សាទុក";
            }
            else
            {
                btnSave.Text = "កែប្រែ";
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}